package com.harmony.pilot.knowledge.entity;

import lombok.Data;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 类定义实体
 * 记录C++类、结构体、枚举等定义
 */
@Data
@Entity
@Table(name = "class_definition", indexes = {
    @Index(name = "idx_class_name", columnList = "class_name"),
    @Index(name = "idx_namespace", columnList = "namespace_name"),
    @Index(name = "idx_class_type", columnList = "class_type")
})
public class ClassDefinition {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /** 所属文件 */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "file_id", nullable = false)
    private CodeFile codeFile;
    
    /** 类名 */
    @Column(name = "class_name", nullable = false, length = 500)
    private String className;
    
    /** 完全限定名 (包含命名空间) */
    @Column(name = "qualified_name", length = 1000)
    private String qualifiedName;
    
    /** 命名空间 */
    @Column(name = "namespace_name", length = 500)
    private String namespaceName;
    
    /** 类型 (class, struct, enum, union, interface) */
    @Column(name = "class_type", nullable = false, length = 20)
    private String classType;
    
    /** 访问修饰符 (public, private, protected) */
    @Column(name = "access_modifier", length = 20)
    private String accessModifier;
    
    /** 是否是抽象类 */
    @Column(name = "is_abstract")
    private Boolean isAbstract = false;
    
    /** 是否是模板类 */
    @Column(name = "is_template")
    private Boolean isTemplate = false;
    
    /** 模板参数 */
    @Column(name = "template_params", columnDefinition = "TEXT")
    private String templateParams;
    
    /** 父类列表 (JSON格式) */
    @Column(name = "base_classes", columnDefinition = "TEXT")
    private String baseClasses;
    
    /** 实现的接口 (JSON格式) */
    @Column(name = "interfaces", columnDefinition = "TEXT")
    private String interfaces;
    
    /** 类注释/文档 */
    @Column(name = "documentation", columnDefinition = "TEXT")
    private String documentation;
    
    /** 类的开始行号 */
    @Column(name = "start_line")
    private Integer startLine;
    
    /** 类的结束行号 */
    @Column(name = "end_line")
    private Integer endLine;
    
    /** 成员变量数量 */
    @Column(name = "field_count")
    private Integer fieldCount = 0;
    
    /** 成员函数数量 */
    @Column(name = "method_count")
    private Integer methodCount = 0;
    
    /** 成员变量列表 */
    @OneToMany(mappedBy = "classDefinition", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<FieldDefinition> fields = new ArrayList<>();
    
    /** 成员函数列表 */
    @OneToMany(mappedBy = "classDefinition", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<MethodDefinition> methods = new ArrayList<>();
    
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
    }
}

